<?php

namespace App\Http\Controllers;

use App\Models\Log;
use App\Models\User;
use App\Transformers\LogTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LogController extends Controller
{
    /**
     * @api {get} /api/log?name=XXX&order=register_time_asc&page=1&limit=2 日志列表
     * @apiGroup Log
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} name  用户名
     * @apiParam {String} ip  ip
     * @apiParam {datetime} start_time  开始时间
     * @apiParam {datetime} end_time  结束后时间
     *
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
            {
                "data": [
                    {
                        "id": 1,
                        "name": "admin",
                        "ip": "123",
                        "content": "测试",
                        "created_at": "",
                        "updated_at": ""
                    },
                    {
                        "id": 2,
                        "name": "admin",
                        "ip": "124",
                        "content": "测试2",
                        "created_at": "",
                        "updated_at": ""
                    }
                ],
                "meta": {
                    "pagination": {
                        "total": 2,
                        "count": 2,
                        "per_page": 10,
                        "current_page": 1,
                        "total_pages": 1,
                        "links": null
                    }
                }
            }
     */
    public function index(Request $request){
        $builder = Log::query();

        if(!Auth::user()->hasPermissionTo('api.log.index.all')){
            $builder->where('user_id',Auth::user()->id);
        }
        if ($name = $request->input('name', '')){
            $user_id = User::where('name',$name)->first();
            if($user_id){
                $builder->where('user_id', $user_id->id);
            }else{
                return response()->json();
            }
        }
        if ($ip = $request->input('ip', '')){
            $builder->where('ip', $ip);
        }
        //登陆时间的比较
        if(($start_time = $request->input('start_time',''))&& $end_time = $request->input('end_time','')){
            $builder->where('created_at', '>', $start_time);
            $builder->where('created_at', '<', $end_time);
        }elseif ($start_time = $request->input('start_time','')){
            $builder->where('created_at', '>', $start_time);
        }elseif ($end_login_time = $request->input('end_time','')){
            $builder->where('created_at', '<', $end_time);
        }
        $users = $builder->orderByDesc('created_at')->paginate($request->get('limit',10));

        return $this->response->paginator($users, new LogTransformer());
    }

    /**
     * @api {get} /api/log/curr?name=XXX&order=register_time_asc&page=1&limit=2 当前登陆日志列表
     * @apiGroup Log
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {String} name  用户名
     * @apiParam {String} ip  ip
     * @apiParam {datetime} start_time  开始时间
     * @apiParam {datetime} end_time  结束后时间
     *
     * @apiParam {int} page   当前页数
     * @apiParam {int} limit  返回总条数
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "name": "admin",
     *                   "ip": "123",
     *                   "content": "测试",
     *                   "created_at": "",
     *                   "updated_at": ""
     *               },
     *               {
     *                   "id": 2,
     *                   "name": "admin",
     *                   "ip": "124",
     *                   "content": "测试2",
     *                   "created_at": "",
     *                   "updated_at": ""
     *           }
     *               ],
     *                   "meta": {
     *                       "pagination": {
     *                       "total": 2,
     *                       "count": 2,
     *                       "per_page": 10,
     *                       "current_page": 1,
     *                       "total_pages": 1,
     *                       "links": null
     *                   }
     *            }
     *       }
     */
    public function currIndex(Request $request){
        $userId = Auth::user()->id;
        $builder = Log::query()->where('user_id',$userId);

        if ($name = $request->input('name', '')){
            $user_id = User::where('name','like',$name)->first()->id;
            $builder->where('user_id', $user_id);
        }
        if ($ip = $request->input('ip', '')){
            $builder->where('ip', $ip);
        }
        //登陆时间的比较
        if(($start_time = $request->input('start_time',''))&& $end_time = $request->input('end_time','')){
            $builder->where('created_at', '>', $start_time);
            $builder->where('created_at', '<', $end_time);
        }elseif ($start_time = $request->input('start_time','')){
            $builder->where('created_at', '>', $start_time);
        }elseif ($end_login_time = $request->input('end_time','')){
            $builder->where('created_at', '<', $end_time);
        }

        $users = $builder->paginate($request->get('limit',10));

        return $this->response->paginator($users, new LogTransformer());
    }
}
